package game.wormgame.mrworm;

import java.util.List;

import game.wormgame.framework.Game;
import game.wormgame.framework.Graphics;
import game.wormgame.framework.Screen;
import game.wormgame.framework.Input.TouchEvent;

public class MainMenuScreen extends Screen
{

	public MainMenuScreen(Game game)
	{
		super(game);
		
	}

	@Override
	public void update(float deltaTime) 
	{
		Graphics g = game.getGraphics();
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();
		
		int len = touchEvents.size();
		
        for(int i = 0; i < len; i++)
        {
            TouchEvent event = touchEvents.get(i);
            if(event.type == TouchEvent.TOUCH_UP) {
                if(inBounds(event, 906, g.getHeight() - 100, 100, 100)) {
                    Settings.soundEnabled = !Settings.soundEnabled;
                    if(Settings.soundEnabled)
                        Assets.meow.play(1);
                }
                if(inBounds(event, 362, 552-392, 267, 92) ) {
                    game.setScreen(new GameScreen(game));
                    if(Settings.soundEnabled)
                        Assets.click.play(1);
                    return;
                }
                if(inBounds(event, 248, 552 -38 - 92, 494, 92) ) {
                    game.setScreen(new HighscoreScreen(game));
                    if(Settings.soundEnabled)
                        Assets.click.play(1);
                    return;
                }
                if(inBounds(event, 362, 552-167-92, 267, 92) ) {
                    game.setScreen(new HelpScreen(game));
                    if(Settings.soundEnabled)
                        Assets.click.play(1);
                    return;
                }
            }
        }
	}

    private boolean inBounds(TouchEvent event, int x, int y, int width, int height) {
        if(event.x > x && event.x < x + width - 1 && 
           event.y > y && event.y < y + height - 1) 
            return true;
        else
            return false;
    }
    
	@Override
	public void present(float deltaTime)
	{
        Graphics g = game.getGraphics();
        
        g.drawPixmap(Assets.background, 0, 0);
        g.drawPixmap(Assets.logo, 50, 50);
        g.drawPixmap(Assets.cat, 634, 552 - 152 - 358);
        g.drawPixmap(Assets.mainMenu, 248, 160);
        if(Settings.soundEnabled)
            g.drawPixmap(Assets.buttons, 906, 441, 0, 0, 100, 100);
        else
            g.drawPixmap(Assets.buttons, 906, 441, 100, 0, 100, 100);
	}

	@Override
	public void pause()
	{
        Settings.save(game.getFileIO());
	}

	@Override
	public void resume()
	{
		
	}

	@Override
	public void dispose()
	{
			
	}

}
